import request from '@/utils/request.js'

/**
 * 动态查询学院、专业、班级
 * @param {Object} options （pageName查询对象，label查询方式，value数据）
 * @returns
 */
// 例如：{pageName: course, label: course_name, value: 'javaEE'}
export function searchCourse(options) {
  const { pageName, label, value, teacher_id } = options
  let data = null
  // 如果有teacher_id，就是教师请求，添加teacher_id作为查询条件
  if (teacher_id) {
    data = {
      [label]: value,
      page_num: options.page_num,
      page_size: options.page_size,
      teacher_id: teacher_id
    }
  } else {
    data = {
      [label]: value,
      page_num: options.page_num,
      page_size: options.page_size
    }
  }
  return request({
    url: `/${pageName}/name`,
    method: 'post',
    data: data
  })
}

// 创建
export function createCourse(options) {
  const { pageName, formData } = options
  return request({
    url: `/${pageName}/create`,
    method: 'post',
    data: formData
  })
}

// 修改课程状态
export function updateCourseState(id) {
  return request({
    url: `/course/update/state/${id}`,
    method: 'put'
  })
}

// 修改课程信息
export function updateCourseInfo(data) {
  return request({
    url: `/course/update/info`,
    method: 'post',
    data: data.formData
  })
}

// 查询主修/选修课程
export function searchCourseByType(data) {
  return request({
    url: '/course/stu/name',
    method: 'post',
    data: {
      [data.label]: data.value,
      type: data.pageName === 'major' ? 1 : 2,
      page_num: data.page_num,
      page_size: data.page_size
    }
  })
}

// 根据id进行查询
export function findById(cid) {
  return request({
    url: `/course/id/${cid}`
  })
}

// 删除课程
export function deleteById(id) {
  return request({
    url: `/course/delete/${id}`,
    method: 'delete'
  })
}
